package com.zx.dao.impl;

import com.zx.bean.Express;
import com.zx.dao.AdminDao;
import com.zx.exception.DuplicateCodeException;
import com.zx.util.DruidUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;


public class AdminDaoImpl  implements AdminDao {
    public static final String SQL_UPDATE_LOGIN_TIME = "update admin  set logintime=?,loginip=? where username=?" ;
    public static final String SQL_LOGIN="select ID from admin where username=? and password=?";
    public static final String SQL_REGISTER="insert into admin (username,password,address,phonenumber,coding)values(?,?,?,?,?)";


    /**
     * 根据用户名,更新登陆时间 ip
     *
     * @param username
     *
     * @param date
     * @param ip
     */
    @Override
    public void updateLoginTime(String username, Date date, String ip) {
        //获取连接
        Connection connection = DruidUtil.getConnection();
        //预编译SQL语句
        PreparedStatement state=null;
        try {
            state = connection.prepareStatement(SQL_UPDATE_LOGIN_TIME);
            state.setDate(1, new java.sql.Date(date.getTime()));
            state.setString(2, ip);
            state.setString(3, username);
            state.executeUpdate();

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(connection, state, null);
        }
    }

    /**
     * 管理员根据账号密码登陆
     *
     * @param username
     * @param password
     * @return
     */
    @Override
    public boolean login(String username, String password) {
        //获取连接
        Connection connection = DruidUtil.getConnection();
        //预编译SQL语句
        PreparedStatement state=null;

        ResultSet resultSet=null;
        try {
            state = connection.prepareStatement(SQL_LOGIN);
            state.setString(1, username);
            state.setString(2, password);
        //拿到结果集,向上抽取
             resultSet = state.executeQuery();
              return resultSet.next();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            DruidUtil.close(connection, state, null);
        }

        return false;

    }
    /**
    * @Author: 吴澳
    * @Description:注册接口
    * @Param:* @param null
    * @return: boolean
    * @Date: 2022/3/4
    */
    @Override
    public boolean register(String username,String password,String address,String phonenumber,String coding) {
        //1 连接的获取
        Connection connection = DruidUtil.getConnection();
        PreparedStatement preparedStatement =null;

        try {
            //2预编译SQL
            preparedStatement=connection.prepareStatement(SQL_REGISTER);
            //3 填充参数
            preparedStatement.setString(1,username);
            preparedStatement.setString(2,password);
            preparedStatement.setString(3,address);
            preparedStatement.setString(4,phonenumber);
            preparedStatement.setString(5,coding);
            //4 执行SQL获取执行结果
            return  preparedStatement.executeUpdate()>0?true:false;
            //6 释放资源
        } catch (SQLException e1) {
         System.out.println(e1.getStackTrace());
        } finally {
            DruidUtil.close(connection,preparedStatement, null);
        }

        return false;
    }

}
